package com.wetao.module_report.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.necer.calendar.BaseCalendar;
import com.necer.calendar.MonthCalendar;
import com.necer.calendar.WeekCalendar;
import com.necer.enumeration.CheckModel;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.enumeration.MultipleCountModel;
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_report.R;

import org.joda.time.LocalDate;

import java.util.List;

/**
 * author: zhouyh
 * created on: 2021/10/26 1:57 下午
 * description:时间选择
 */
public class DateDialogFragment extends DialogFragment implements View.OnClickListener {


    private TextView tv_curMonth;
    private TextView tv_preYear,tv_nextYear,tv_preMonth,tv_nextMonth;
    private static final String TAG = DateDialogFragment.class.getCanonicalName();
    private MonthCalendar monthCalendar;
    protected int type;
    private OnDateSetListener dateSetListener;

    private Button btn_cancel;
    private Button btn_ok;

    private int mYear;
    private int mMonth;
    private LocalDate mLocalDate;
    private List<LocalDate> mCheckList;


    public static DateDialogFragment getInstance(int type) {
        Bundle bundle = new Bundle();
        DateDialogFragment dialogFragment = new DateDialogFragment();
        bundle.putInt("date_type",type);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.875), (int) (dm.heightPixels * 0.875));
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.report_date_picker, container);
        tv_curMonth = rootView.findViewById(R.id.tv_cur_month);
        monthCalendar = rootView.findViewById(R.id.date_picker);
        btn_ok = rootView.findViewById(R.id.yes_btn);
        btn_cancel = rootView.findViewById(R.id.no_btn);
        tv_preYear = rootView.findViewById(R.id.tv_pre_year);
        tv_preMonth = rootView.findViewById(R.id.tv_pre_month);
        tv_nextYear = rootView.findViewById(R.id.tv_next_year);
        tv_nextMonth = rootView.findViewById(R.id.tv_next_month);

        btn_cancel.setOnClickListener(this);
        btn_ok.setOnClickListener(this);

        tv_preYear.setOnClickListener(this);
        tv_preMonth.setOnClickListener(this);
        tv_nextMonth.setOnClickListener(this);
        tv_nextYear.setOnClickListener(this);

        return rootView;

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        type = getArguments().getInt("date_type",1);
        monthCalendar.setCheckMode(type == 2 ? CheckModel.MULTIPLE:CheckModel.SINGLE_DEFAULT_CHECKED);
        mLocalDate = new LocalDate();
        if (type == 2){
            monthCalendar.setMultipleCount(2, MultipleCountModel.FULL_REMOVE_FIRST);
        }
        monthCalendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
            @Override
            public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate, DateChangeBehavior dateChangeBehavior) {
                tv_curMonth.setText(year + "年" + month + "月");
                mYear = year;
                mMonth = month;
                mLocalDate = localDate;
                Log.d(TAG, "setOnCalendarChangedListener:::" + year + "年" + month + "月" + "   当前页面选中 " + localDate);
                Log.e(TAG, "baseCalendar::" + baseCalendar);
            }

        });


        monthCalendar.setOnCalendarMultipleChangedListener(new OnCalendarMultipleChangedListener() {
            @Override
            public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, List<LocalDate> currPagerCheckedList, List<LocalDate> totalCheckedList, DateChangeBehavior dateChangeBehavior) {
                tv_curMonth.setText(year + "年" + month + "月");
                mYear = year;
                mMonth = month;
                mCheckList = currPagerCheckedList;
                Log.d(TAG, year + "年" + month + "月");
                Log.d(TAG, "当前页面选中：：" + currPagerCheckedList);
                Log.d(TAG, "全部选中：：" + totalCheckedList);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
       if (id == R.id.no_btn) {
            dismiss();
        } else if (id == R.id.yes_btn) {
           if (dateSetListener!=null){
               if (type == 1){
                   dateSetListener.onDateSet(mYear,mMonth,mLocalDate);
                   dismiss();
               }else if (type == 3){
                   dateSetListener.onMonthDateSet(mYear,mMonth);
                   dismiss();
               }else if (type == 2){
                   if (mCheckList.size() != 2){
                       ToastUtil.initToast("请选择2个日期");
                       return;
                   }
                   dateSetListener.onWeekDateSet(mYear,mMonth,mCheckList);
                   dismiss();
               }
           }
        }else if (id == R.id.tv_pre_year){
           monthCalendar.jumpDate(mYear-1,mMonth,mLocalDate.getDayOfMonth());
       }else if (id == R.id.tv_next_year){
           monthCalendar.jumpDate(mYear+1,mMonth,mLocalDate.getDayOfMonth());
       }else if (id == R.id.tv_pre_month){
           monthCalendar.toLastPager();
       }else if (id == R.id.tv_next_month){
           monthCalendar.toNextPager();
       }
    }



    public void setDateSetListener(OnDateSetListener dateSetListener) {
        this.dateSetListener = dateSetListener;
    }

    public interface OnDateSetListener {
        void onMonthDateSet(int year, int month);
        void onDateSet(int year,int month,LocalDate localDate);
        void onWeekDateSet(int year, int month, List<LocalDate> currPagerCheckedList);
    }

}
